﻿<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<CallMVC.Models.LoginModel>" %>




<asp:Content ID="calcInputTitle" ContentPlaceHolderID="Title1" runat="server">
      <%
          
              
          DBManager.Customer cust = ViewBag.Customer;
      
  
       %>
   <a href="<%: Url.Action("Calculation", "Home", new { customerId = cust.CustomerID })%>"><img src="/CalcApp/images/<%= cust.LogoLink %>" /></a>
  
</asp:Content>

<asp:Content ID="CalcInputContent" ContentPlaceHolderID="MainContent" runat="server">

    <%
        
        try
        {
            DBManager.DBManager.Instance.mutexlock();

         %>
		<script language="JavaScript">

		    $(document).ready(function () {
		        var targets = $('[rel~=tooltip]'),
        target = false,
        tooltip = false,
        title = false;

		        targets.bind('mouseenter', function () {
		            target = $(this);
		            tip = target.attr('title');
		            tooltip = $('<div id="tooltip"></div>');

		            if (!tip || tip == '')
		                return false;

		            target.removeAttr('title');
		            tooltip.css('opacity', 0)
               .html(tip)
               .appendTo('body');

		            var init_tooltip = function () {
		                if ($(window).width() < tooltip.outerWidth() * 1.5)
		                    tooltip.css('max-width', $(window).width() / 2);
		                else
		                    tooltip.css('max-width', 340);

		                var pos_left = target.offset().left + (target.outerWidth() / 2) - (tooltip.outerWidth() / 2),
                pos_top = target.offset().top - tooltip.outerHeight() - 20;

		                if (pos_left < 0) {
		                    pos_left = target.offset().left + target.outerWidth() / 2 - 20;
		                    tooltip.addClass('left');
		                }
		                else
		                    tooltip.removeClass('left');

		                if (pos_left + tooltip.outerWidth() > $(window).width()) {
		                    pos_left = target.offset().left - tooltip.outerWidth() + target.outerWidth() / 2 + 20;
		                    tooltip.addClass('right');
		                }
		                else
		                    tooltip.removeClass('right');

		                if (pos_top < 0) {
		                    var pos_top = target.offset().top + target.outerHeight();
		                    tooltip.addClass('top');
		                }
		                else
		                    tooltip.removeClass('top');

		                tooltip.css({ left: pos_left, top: pos_top })
                   .animate({ top: '+=10', opacity: 1 }, 50);
		            };

		            init_tooltip();
		            $(window).resize(init_tooltip);

		            var remove_tooltip = function () {
		                tooltip.animate({ top: '-=10', opacity: 0 }, 50, function () {
		                    $(this).remove();
		                });

		                target.attr('title', tip);
		            };

		            target.bind('mouseleave', remove_tooltip);
		            tooltip.bind('click', remove_tooltip);
		        });
		    });

		    var currentNum = null;
		    function numberWithCommas(x) {

		        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");

		    }

		    function chkNum(ele, isPercent) {

		        return true;
		    }

		    function onMouseOver(ele, isPercent) {

		        var value = ele.value;

		        var isText = false;
		        if (ele.type == "text") {
		            ele.blur();
		            ele.type = "number";
		            isText = true;
		        }

		        if (currentNum != null && ele != currentNum)
		            currentNum.type = "text";


		        currentNum = ele;

		        var onfocus = ele.onfocus;
		        var onblur = ele.onblur;
		        ele.onblur = null;
		        ele.onfocus = null;

		        ele.focus();

		        ele.onfocus = onfocus;
		        ele.onblur = onblur;

		        if (isText == true) {
		            value = value.replace("%", "");
		            value = value.replace(/\./g, "");
		            value = value.replace(/\,/g, ".");



		            ele.value = parseFloat(value);


		          //  alert(ele.value);
		            if (isPercent == true)
		                ele.value = ele.value;
		        }


		        return true;
		    }

		    function onFocus(ele, isPercent) {

		        var value = ele.value;

		        var isText = false;
		        if (ele.type == "text") {
		            ele.type = "number";
		            isText = true;
		        }

		        if (currentNum != null && ele != currentNum)
		            currentNum.type = "text";


		        currentNum = ele;




		        if (isText == true) {
		            value = value.replace("%", "");
		            value = value.replace(/\./g, "");
		            value = value.replace(/\,/g, ".");



		            ele.value = parseFloat(value);


		            //  alert(ele.value);
		            if (isPercent == true)
		                ele.value = ele.value;
		        }
		        return true;
		    }

		    function onBlur(ele, isPercent,nDp) {
		        ele.type = "text";

		        if (isPercent == true) {
		            var value = ele.value.toString();
		            //alert(value);
		            value = value;
		            var val = value.toString();

		            var value = "";

		            if (val != "") {
		                var xa = parseFloat(val);
		                var n;
		                if (nDp > 0)
		                    n = xa.toFixed(nDp - 1);
		                else
		                    n = xa;

		                val = n.toString();
		            }

		            value = val.replace(".", ",");
		            ele.value = value + "%";
		        }
		        else {

		            var value = "";

		            if ( ele.value != "") {
		                var xa = parseFloat(ele.value);
		                var n = xa.toFixed(nDp);

		                value = n.toString();
		            }

		            value = value.replace(".", ",");
		            ele.value = numberWithCommas(value);
		        }

		        return true;
		    }

		    function OnProp(ele, isPercent) {
		        //ele.focus();
		        return true;
		    }
		</script>
    <% 
        
        DBManager.Calculation calc = (DBManager.Calculation)this.ViewBag.Calculation;
        

        using (Html.BeginForm("CalcOut", "Home", new { calculationId = calc.CalculationID }))
        {
            bool isLoadOrder = ViewBag.isLoadOrder;
         
            DBManager.OrderControl orderControl = ViewBag.OrderControl;
            System.Collections.Generic.Dictionary<string, string> inputDicts = null;

            if (isLoadOrder)
            {
                inputDicts = orderControl.InputDicts;
            }                     
    %>
    <%: Html.ValidationSummary()%>

    <ul data-role="listview" data-inset="true">
        <li data-role="fieldcontain">  <h3><%: Html.Label(((DBManager.Calculation)this.ViewBag.Calculation).CaculationName)%></h3> </li> 

<%     
            bool Access = true;
            if (User.Identity.IsAuthenticated)
            {
                DBManager.User user = DBManager.DBManager.Instance.GetUser(User.Identity.Name);

                Access = user.CanAccess(calc);
            }


            if (Access == false)
            {                    
%>

<br />
  <center>Access denied.</center>



    <%
            }

            else
            {
                
               %>


           <%

                foreach (DBManager.InputDesignGroup group in calc.InputDesignGroupsList)
                {
       
    %>

    <li>
    <div data-role=content>
       
    <h3><%: Html.Label(group.GroupDescription)%></h3>

    <%  
                    DBManager.Column itemColumn;

                    if (group.ColumnList.Count == 0)
                    {

                        if (group.inputDesignList.Count > 0 && group.inputDesignList[0].FieldID == null)
                        {
                        
             %>
             <div>
                                 <div class="ui-block-a"></div>
	                <div class="ui-block-b"></div>
                        <%
                        }
                        else
                        {
                                
                                %>
    
                
                <div class="ui-grid-a"  >
                    <div class="ui-block-a"></div>
	                <div class="ui-block-b"></div>
    <%
                        }
                    }
                    else if (group.ColumnList.Count == 1)
                    {
    %>       
                <div class="ui-grid-a"  >
                    <div class="ui-block-a"></div>
	                <div class="ui-block-b">
                        <% 
                        itemColumn = group.ColumnList[0];
                        %>                        
                        <center> <h3> <%: itemColumn.Headline%></h3></center>
                    </div>
    <%                 
                    }
                    else if (group.ColumnList.Count == 2)
                    {

                        if (group.inputDesignList[0].FieldDescription != "")
                        {
    %>
                <div class="ui-grid-b"  >
                    <div class="ui-block-a"></div>
	                <div class="ui-block-b">
                        <% 
                            itemColumn = group.ColumnList[0];
                        %>                         
                        <center><h3> <%: itemColumn.Headline%></h3></center>
                    </div>
                    <div class="ui-block-c">
                        <% 
                            itemColumn = group.ColumnList[1];
                        %> 
                        <center> <h3> <%: itemColumn.Headline%></h3></center>
                    </div> 
    <%    
                        }
                        else
                        {
    %>         
                    <div class="ui-grid-a"  >
                    <div class="ui-block-a">
                        <% 
                            itemColumn = group.ColumnList[0];
                        %>                         
                        <center><h3> <%: itemColumn.Headline%></h3></center>                    
                    </div>
	                <div class="ui-block-b">
                        <% 
                            itemColumn = group.ColumnList[1];
                        %> 
                        <center> <h3> <%: itemColumn.Headline%></h3></center>
                    </div>
                     
    <%
                        }
                    }
                    else if (group.ColumnList.Count == 3)
                    {
    %>
                <div class="ui-grid-c"  >
                    <div class="ui-block-a" ></div>
	                <div class="ui-block-b">
                        <% 
                        itemColumn = group.ColumnList[0];
                        %>
                       <center> <h3> <%: itemColumn.Headline%></h3></center></div>
                    <div class="ui-block-c">
                        <% 
                        itemColumn = group.ColumnList[1];
                        %>
                        <center><h3> <%: itemColumn.Headline%></h3></center></div>
                    <div class="ui-block-d">
                        <% 
                        itemColumn = group.ColumnList[2];
                        %>
                         <center><h3> <%: itemColumn.Headline%></h3></center></div>
    <%                   
                    }
                    else if (group.ColumnList.Count == 4)
                    {
    %>
                <div class="ui-grid-d"  >
                    <div class="ui-block-a"></div>
	                <div class="ui-block-b">
                        <% 
                        itemColumn = group.ColumnList[0];
                        %>
                       <center><h3> <%: itemColumn.Headline%></h3></center></div>
                    <div class="ui-block-c">
                        <% 
                        itemColumn = group.ColumnList[1];
                        %>
                       <center> <h3> <%: itemColumn.Headline%></h3></center></div>
                    <div class="ui-block-d">
                        <% 
                        itemColumn = group.ColumnList[2];
                        %>
                        <center><h3> <%: itemColumn.Headline%></h3></center></div>
                    <div class="ui-block-e">
                        <% 
                        itemColumn = group.ColumnList[3];
                        %>
                       <center> <h3> <%: itemColumn.Headline%></h3></center></div>                  
    <% 
                    }        
    %>
    <%   
                    foreach (DBManager.InputDesign input in group.inputDesignList)
                    {

                        %>



                       <%
                        if ((input.ColumnNu == 1 || input.ColumnNu == null) && (input.FieldDescription != ""))
                        {
                            if (input.FieldID == null)
                            {
                            %>
                           <div> <label>
    <%                      
                            if (input.Comment != "")
                            { 
    %>
                                <abbr title="<%= input.Comment %>" rel="tooltip">
    <% 
                            }    
    %>
                                <%: input.FieldDescription%>
                                    
    <%                      
                            if (input.Comment != "")
                            { 
    %>
                                </abbr>
    <% 
                            }    
    %>
                                </label></div>
                            <%}
                            else
                            {
                                
    %>
              <div class="ui-block-a" ><label>
    <%                      
                            if (input.Comment != "")
                            { 
    %>
                                <abbr title="<%= input.Comment %>" rel="tooltip">
    <% 
                            }    
    %>
                               <%: input.FieldDescription%>
    <%                      
                            if (input.Comment != "")
                            { 
    %>
                                </abbr>
    <% 
                            }    
    %>
          </label></div>
    <%}
                        }
                                                
                        if ((group.ColumnList.Count > 0))
                        {
                            int count = 0;
                            if(input.ColumnNu != null)
                                count = (int)input.ColumnNu;
                            if (input.FieldDescription != "")
                            {                                                        
    %>

    <% 
                                string valueLoad = input.defaultValueFormat;

                                if (inputDicts != null && input.FieldID != null)
                                {
                                    if (inputDicts.ContainsKey(input.FieldID))
                                        valueLoad = inputDicts[input.FieldID];
                                }

                                if (input.ListID == null)
                                {
                                    if (count == 1)
                                    {
    %>                                    
                <div class="ui-block-b" style="white-space:normal;">
    <% 
                                    }
                                    else if (count == 2)
                                    {
    %>
                <div class="ui-block-c" style="white-space:normal;">
    <% 
                                    }
                                    else if (count == 3)
                                    {
    %>
                <div class="ui-block-d" style="white-space:normal;">
    <% 
                                    }
                                    else if (count == 4)
                                    {
    %>
                <div class="ui-block-e" style="white-space:normal;">
    <% 
                                    }

                                    if (input.isNumber)
                                    {
                                        string js_function = "JavaScript:chkNum(this," + input.isPercent.ToString().ToLower() + ")";
                                        string js_onfocus = "JavaScript:onFocus(this," + input.isPercent.ToString().ToLower() + ")";
                                        string js_onblur = "JavaScript:onBlur(this," + input.isPercent.ToString().ToLower() + " , " + input.nDp + ")";
                                        string js_onprop = "JavaScript:onMouseOver(this," + input.isPercent.ToString().ToLower() + ")";                                                                 
    %>
                     <% if (input.isPercent)
                        { %>

                    <%: Html.TextBox(input.FieldID, valueLoad, new { @type = "text", @onfocus = js_onfocus, @onChange = js_function, @style = "text-align:right;width:85%;", @onblur = js_onblur, @onmouseover = js_onprop })%>
                
                    <% }
                        else
                        { %>

                        <%: Html.TextBox(input.FieldID, valueLoad, new { @type = "text", @onfocus = js_onfocus, @onChange = js_function, @style = "text-align:right;width:85%;", @onblur = js_onblur, @onmouseover = js_onprop })%>

                    <% } %>
    <% 
                                    }
                                    else if (input.isDateTime)
                                    {
                                        DateTime date = DateTime.ParseExact(valueLoad, input.NumberFormat.Replace("mm", "MM"), null);
                                        valueLoad = date.ToString("yyyy-MM-dd");
  %>
  <%: Html.TextBox(input.FieldID, valueLoad, new { @type = "date", @style = "width:85%;" })%>
  <%
                                    }
                                    else if (input.FieldID != null)
                                    { 
    %>
                    <%: Html.TextBox(input.FieldID, valueLoad, new { @style = "width:85%;" })%>

    <% 
        
                                    }
                                    
                if(input.FieldID != null){                    
    %>
               
                </div>
    <%  
    }
                                }
                                else
                                {
    %>
    <% 
                                    if (count == 1)
                                    {
    %>                                    
                <div class="ui-block-b">
    <% 
                                    }
                                    else if (count == 2)
                                    {
    %>
                <div class="ui-block-c" >
    <% 
                                    }
                                    else if (count == 3)
                                    {
    %>
                <div class="ui-block-d" >
    <% 
                                    }
                                    else if (count == 4)
                                    {
    %>
                <div class="ui-block-e" style="text-align:left" >
    <% 
                                    }                                                                
    %>
    <%  if (input.InputList.Count > 2)
        { %>
                                <select class="left_align" name="<%= input.FieldID %>" id="Select1" style = "text-align:left;width:85%;">
                                   
    <% 
        }else
        {
            
            %>
            <select class="left_align" name="<%= input.FieldID %>" id="Select2" style = "text-align:left;width:85%;" data-role="slider">
            <%
            }                                 
                                    foreach (DBManager.InputDesignList item in input.InputList)
                                    {
                                        string selected = "";
                                        if (item.ItemID == valueLoad)
                                            selected = "selected";
                                    
    %>
                                <option style="text-align:left" value="<%= item.ItemID %>" <%= selected %> ><%= item.ItemID%></option>
    <%
                                   
                                    }                                                           
    %>
    
                               </select>
                </div>    
    <% 
                                }
                                count++; 
    %>
    <%  
                            }
                            else
                            {
    %>
    <% 
                                string valueLoad = input.defaultValueFormat;

                                if (inputDicts != null)
                                {
                                    if (inputDicts.ContainsKey(input.FieldID))
                                        valueLoad = inputDicts[input.FieldID];
                                }

                                if (input.ListID == null)
                                {
                                    if (count == 1)
                                    {
    %>                                    
                <div class="ui-block-a" style="white-space:normal;">
    <% 
                                    }
                                    else if (count == 2)
                                    {
    %>
                <div class="ui-block-b" style="white-space:normal;">
    <% 
                                    }
                                    else if (count == 3)
                                    {
    %>
                <div class="ui-block-c" style="white-space:normal;">
    <% 
                                    }
                                    else if (count == 4)
                                    {
    %>
                <div class="ui-block-d" style="white-space:normal;">
    <% 
                                    }

                                    if (input.isNumber)
                                    {

                                        string js_function = "JavaScript:chkNum(this," + input.isPercent.ToString().ToLower() + ")";
                                        string js_onfocus = "JavaScript:onFocus(this," + input.isPercent.ToString().ToLower() + ")";
                                        string js_onblur = "JavaScript:onBlur(this," + input.isPercent.ToString().ToLower() + " , " + input.nDp + ")";
                                        string js_onprop = "JavaScript:onMouseOver(this," + input.isPercent.ToString().ToLower() + ")";                                                                    
    %>
                   <%: Html.TextBox(input.FieldID, valueLoad, new { @type = "text", @onfocus = js_onfocus, @onChange = js_function, @style = "text-align:right;width:85%;", @onblur = js_onblur, @onmouseover = js_onprop })%>
                
    <%
                                    }
                                    else if (input.isDateTime)
                                    {
                                        DateTime date = DateTime.ParseExact(valueLoad, input.NumberFormat.Replace("mm", "MM"), null);
                                        valueLoad = date.ToString("yyyy-MM-dd");
  %>
  <%: Html.TextBox(input.FieldID, valueLoad, new { @type = "date", @style = "width:85%;" })%>
  <%
                                    }

                                    else
                                    { 
    %>
                   <%: Html.TextBox(input.FieldID, valueLoad, new { @style = "width:85%;" })%>

    <% 
                                    } 
    %>
                </div>
    <%  
                                }
                                else
                                {
                                    System.Collections.Generic.List<SelectListItem> items = new System.Collections.Generic.List<SelectListItem>();
          

    %>
    <% 
                                    if (count == 1)
                                    {
    %>                                    
                <div class="ui-block-a" style="text-align:left">
    <% 
                                    }
                                    else if (count == 2)
                                    {
    %>
                <div class="ui-block-b" style="text-align:left">
    <% 
                                    }
                                    else if (count == 3)
                                    {
    %>
                <div class="ui-block-c" style="text-align:left">
    <% 
                                    }
                                    else if (count == 4)
                                    {
    %>
                <div class="ui-block-d" style="text-align:left">
    <% 
                                    }
                                    
    %>

    <%  if (input.InputList.Count > 2)
        { %>
                                <select class="left_align" name="<%= input.FieldID %>" id="<%= input.FieldID %>" style = "text-align:left;width:85%;">
                                   
    <% 
        }else
        {
            
            %>
            <select class="left_align" name="<%= input.FieldID %>" id="Select2" style = "text-align:left;width:85%;" data-role="slider">
            <%
            }
                                               
                                    foreach (DBManager.InputDesignList item in input.InputList)
                                    {
                                        string selected = "";
                                        if (item.ItemID == valueLoad)
                                            selected = "selected";
                                    
    %>
                                <option style="text-align:left" value="<%= item.ItemID %>" <%= selected %> ><%= item.ItemID%></option>
    <%
                                   
                                    }                                                           
    %>
    
                               </select>
                </div>    


    <% 
                                }
                                
                                %>


                    <%

                                count++; 
    %>

    <%                            
                            }
                        }
                        else
                        { 
    %>

    <% 
                            string valueLoad = input.defaultValueFormat;

                            if (inputDicts != null && input.FieldID != null)
                            {
                                if (inputDicts.ContainsKey(input.FieldID))
                                    valueLoad = inputDicts[input.FieldID];
                            }



                            if (input.ListID == null)
                            { 
    %>
                <div class="ui-block-b" style="white-space:normal;">

    <%
                                if (input.isNumber)
                                {
                                    string js_function = "JavaScript:chkNum(this," + input.isPercent.ToString().ToLower() + ")";
                                    string js_onfocus = "JavaScript:onFocus(this," + input.isPercent.ToString().ToLower() + ")";
                                    string js_onblur = "JavaScript:onBlur(this," + input.isPercent.ToString().ToLower() + " , " + input.nDp + ")";
                                    string js_onprop = "JavaScript:onMouseOver(this," + input.isPercent.ToString().ToLower() + ")";     
                                    %>
                   <%: Html.TextBox(input.FieldID, valueLoad, new { @type = "text", @onfocus = js_onfocus, @onChange = js_function, @style = "text-align:right;width:85%;", @onblur = js_onblur, @onmouseover = js_onprop })%>
                
    <% 
                                }
                                else if (input.isDateTime)
                                {
                                    DateTime date = DateTime.ParseExact(valueLoad, input.NumberFormat.Replace("mm", "MM"), null);
                                    valueLoad = date.ToString("yyyy-MM-dd");
  %>
  <%: Html.TextBox(input.FieldID, valueLoad, new { @type = "date", @style = "width:85%;" })%>
  <%
                                
                                }
                                else if (input.FieldID != null)
                                { 
    %>
                   <%: Html.TextBox(input.FieldID, valueLoad, new { @style = "width:85%;" })%>

    <% 
                                }

    %>
                </div>
    <%  
                            }
                            else
                            {
                            
 
    %>
                <div class="ui-block-b" style="text-align:left">    
    <%  if (input.InputList.Count > 2)
        { %>
                                <select class="left_align" name="<%= input.FieldID %>" id="Select3" style = "text-align:left;width:85%;">
                                   
    <% 
        }else
        {
            
            %>
            <select class="left_align" name="<%= input.FieldID %>" id="Select2" style = "text-align:left;width:85%;" data-role="slider">
            <%
            }                                
                                foreach (DBManager.InputDesignList item in input.InputList)
                                {
                                    string selected = "";
                                    if (item.ItemID == valueLoad)
                                        selected = "selected";
                                    
    %>
                                <option style="text-align:left" value="<%= item.ItemID %>" <%= selected %> ><%= item.ItemID%></option>
    <%
                                   
                                }                                                           
    %>
    
                               </select>
                </div>
    <%
                            } 
    %>    

    <%
                        }     
    %>
                   
                    
    <%                  
          
                    }
    %>



</li>
    
    <% 
                }

                string calText = "Calculation";

                if (calc.TextCalcButton != null)
                    calText = calc.TextCalcButton;
    %>         
        

    <%
                if (User.Identity.IsAuthenticated)
                {
                    string saveCalc = "Save Calculation";

                    if (calc.TextSaveButton != null)
                        saveCalc = calc.TextSaveButton;
                
    %>
                <li data-role="fieldcontain">
                <%: Html.Label("SaveCalc", saveCalc)%>
                <%: Html.CheckBox("SaveCalc")%>
            </li>
    <%   
                } 
    %>
            <li data-role="fieldcontain">

                <input type="submit" value="<%= calText %>" />

                </li>

            

                <% } %>

        </ul>


    <%
        }

        DBManager.DBManager.Instance.mutexUnlock();

        }
        catch (Exception)
        {
            DBManager.DBManager.Instance.mutexUnlock();
            throw;
        }
    %>
    
</asp:Content>
